package com.iknow.view.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.iknow.util.Loger;

public class PullToRefreshListView extends ListView
  implements AbsListView.OnScrollListener
{
  private static final int PULL_TO_REFRESH = 2;
  private static final int REFRESHING = 4;
  private static final int RELEASE_TO_REFRESH = 3;
  private static final String TAG = "PullToRefreshListView";
  private static final int TAP_TO_REFRESH = 1;
  private boolean mBounceHack;
  private int mCurrentScrollState;
  private RotateAnimation mFlipAnimation;
  private LayoutInflater mInflater;
  private int mLastMotionY;
  private OnRefreshListener mOnRefreshListener;
  private AbsListView.OnScrollListener mOnScrollListener;
  private int mRefreshOriginalTopPadding;
  private int mRefreshState;
  private RelativeLayout mRefreshView;
  private int mRefreshViewHeight;
  private ImageView mRefreshViewImage;
  private TextView mRefreshViewLastUpdated;
  private ProgressBar mRefreshViewProgress;
  private TextView mRefreshViewText;
  private RotateAnimation mReverseFlipAnimation;

  public PullToRefreshListView(Context paramContext)
  {
    super(paramContext);
    init(paramContext);
  }

  public PullToRefreshListView(Context paramContext, AttributeSet paramAttributeSet)
  {
    super(paramContext, paramAttributeSet);
    init(paramContext);
  }

  public PullToRefreshListView(Context paramContext, AttributeSet paramAttributeSet, int paramInt)
  {
    super(paramContext, paramAttributeSet, paramInt);
    init(paramContext);
  }

  private void applyHeaderPadding(MotionEvent paramMotionEvent)
  {
    int i = paramMotionEvent.getHistorySize();
    int j = 0;
    while (true)
    {
      if (j >= i)
        return;
      if (this.mRefreshState == 3)
      {
        if (isVerticalFadingEdgeEnabled())
          setVerticalScrollBarEnabled(false);
        int k = (int)(((int)paramMotionEvent.getHistoricalY(j) - this.mLastMotionY - this.mRefreshViewHeight) / 1.7D);
        this.mRefreshView.setPadding(this.mRefreshView.getPaddingLeft(), k, this.mRefreshView.getPaddingRight(), this.mRefreshView.getPaddingBottom());
      }
      ++j;
    }
  }

  private void init(Context paramContext)
  {
    this.mFlipAnimation = new RotateAnimation(0F, -180.0F, 1, 0.5F, 1, 0.5F);
    this.mFlipAnimation.setInterpolator(new LinearInterpolator());
    this.mFlipAnimation.setDuration(250L);
    this.mFlipAnimation.setFillAfter(true);
    this.mReverseFlipAnimation = new RotateAnimation(-180.0F, 0F, 1, 0.5F, 1, 0.5F);
    this.mReverseFlipAnimation.setInterpolator(new LinearInterpolator());
    this.mReverseFlipAnimation.setDuration(250L);
    this.mReverseFlipAnimation.setFillAfter(true);
    this.mInflater = ((LayoutInflater)paramContext.getSystemService("layout_inflater"));
    this.mRefreshView = ((RelativeLayout)this.mInflater.inflate(2130903115, this, false));
    this.mRefreshViewText = ((TextView)this.mRefreshView.findViewById(2131296511));
    this.mRefreshViewImage = ((ImageView)this.mRefreshView.findViewById(2131296510));
    this.mRefreshViewProgress = ((ProgressBar)this.mRefreshView.findViewById(2131296509));
    this.mRefreshViewLastUpdated = ((TextView)this.mRefreshView.findViewById(2131296512));
    this.mRefreshViewImage.setMinimumHeight(50);
    this.mRefreshView.setOnClickListener(new OnClickRefreshListener(this, null));
    this.mRefreshOriginalTopPadding = this.mRefreshView.getPaddingTop();
    this.mRefreshState = 1;
    addHeaderView(this.mRefreshView);
    super.setOnScrollListener(this);
    measureView(this.mRefreshView);
    this.mRefreshViewHeight = this.mRefreshView.getMeasuredHeight();
  }

  private void measureView(View paramView)
  {
    int k;
    ViewGroup.LayoutParams localLayoutParams = paramView.getLayoutParams();
    if (localLayoutParams == null)
      localLayoutParams = new ViewGroup.LayoutParams(-1, -2);
    int i = ViewGroup.getChildMeasureSpec(0, 0, localLayoutParams.width);
    int j = localLayoutParams.height;
    if (j > 0)
      k = View.MeasureSpec.makeMeasureSpec(j, 1073741824);
    while (true)
    {
      paramView.measure(i, k);
      return;
      k = View.MeasureSpec.makeMeasureSpec(0, 0);
    }
  }

  private void resetHeader()
  {
    if (this.mRefreshState != 1)
    {
      this.mRefreshState = 1;
      resetHeaderPadding();
      this.mRefreshViewText.setText(2131361851);
      this.mRefreshViewImage.setImageResource(2130837590);
      this.mRefreshViewImage.clearAnimation();
      this.mRefreshViewImage.setVisibility(8);
      this.mRefreshViewProgress.setVisibility(8);
    }
  }

  private void resetHeaderPadding()
  {
    this.mRefreshView.setPadding(this.mRefreshView.getPaddingLeft(), this.mRefreshOriginalTopPadding, this.mRefreshView.getPaddingRight(), this.mRefreshView.getPaddingBottom());
  }

  protected void onAttachedToWindow()
  {
    setSelection(1);
  }

  public void onRefresh()
  {
    Loger.d("PullToRefreshListView", "onRefresh");
    if (this.mOnRefreshListener != null)
      this.mOnRefreshListener.onRefresh();
  }

  public void onRefreshComplete()
  {
    Loger.d("PullToRefreshListView", "onRefreshComplete");
    resetHeader();
    if (this.mRefreshView.getBottom() > 0)
    {
      invalidateViews();
      setSelection(1);
    }
  }

  public void onRefreshComplete(CharSequence paramCharSequence)
  {
    setLastUpdated(paramCharSequence);
    onRefreshComplete();
  }

  public void onScroll(AbsListView paramAbsListView, int paramInt1, int paramInt2, int paramInt3)
  {
    if ((this.mCurrentScrollState == 1) && (this.mRefreshState != 4))
      if (paramInt1 == 0)
      {
        this.mRefreshViewImage.setVisibility(0);
        if ((((this.mRefreshView.getBottom() >= 20 + this.mRefreshViewHeight) || (this.mRefreshView.getTop() >= 0))) && (this.mRefreshState != 3))
        {
          this.mRefreshViewText.setText(2131361849);
          this.mRefreshViewImage.clearAnimation();
          this.mRefreshViewImage.startAnimation(this.mFlipAnimation);
          this.mRefreshState = 3;
        }
      }
    while (true)
    {
      do
        while (true)
        {
          while (true)
          {
            while (true)
            {
              do
              {
                if (this.mOnScrollListener != null)
                  this.mOnScrollListener.onScroll(paramAbsListView, paramInt1, paramInt2, paramInt3);
                return;
              }
              while ((this.mRefreshView.getBottom() >= 20 + this.mRefreshViewHeight) || (this.mRefreshState == 2));
              this.mRefreshViewText.setText(2131361848);
              if (this.mRefreshState != 1)
              {
                this.mRefreshViewImage.clearAnimation();
                this.mRefreshViewImage.startAnimation(this.mReverseFlipAnimation);
              }
              this.mRefreshState = 2;
            }
            this.mRefreshViewImage.setVisibility(8);
            resetHeader();
          }
          if ((this.mCurrentScrollState != 2) || (paramInt1 != 0) || (this.mRefreshState == 4))
            break;
          setSelection(1);
          this.mBounceHack = true;
        }
      while ((!(this.mBounceHack)) || (this.mCurrentScrollState != 2));
      setSelection(1);
    }
  }

  public void onScrollStateChanged(AbsListView paramAbsListView, int paramInt)
  {
    this.mCurrentScrollState = paramInt;
    if (this.mCurrentScrollState == 0)
      this.mBounceHack = false;
    if (this.mOnScrollListener != null)
      this.mOnScrollListener.onScrollStateChanged(paramAbsListView, paramInt);
  }

  public boolean onTouchEvent(MotionEvent paramMotionEvent)
  {
    int i = (int)paramMotionEvent.getY();
    this.mBounceHack = false;
    switch (paramMotionEvent.getAction())
    {
    default:
    case 1:
    case 0:
    case 2:
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          do
            while (true)
            {
              do
              {
                return super.onTouchEvent(paramMotionEvent);
                if (!(isVerticalScrollBarEnabled()))
                  setVerticalScrollBarEnabled(true);
              }
              while ((getFirstVisiblePosition() != 0) || (this.mRefreshState == 4));
              if (((this.mRefreshView.getBottom() < this.mRefreshViewHeight) && (this.mRefreshView.getTop() < 0)) || (this.mRefreshState != 3))
                break;
              this.mRefreshState = 4;
              prepareForRefresh();
              onRefresh();
            }
          while ((this.mRefreshView.getBottom() >= this.mRefreshViewHeight) && (this.mRefreshView.getTop() > 0));
          resetHeader();
          setSelection(1);
        }
        this.mLastMotionY = i;
      }
      applyHeaderPadding(paramMotionEvent);
    }
  }

  public void prepareForRefresh()
  {
    resetHeaderPadding();
    this.mRefreshViewImage.setVisibility(8);
    this.mRefreshViewImage.setImageDrawable(null);
    this.mRefreshViewProgress.setVisibility(0);
    this.mRefreshViewText.setText(2131361850);
    this.mRefreshState = 4;
  }

  public void removeHeadView()
  {
    removeHeaderView(this.mRefreshView);
  }

  public void setAdapter(ListAdapter paramListAdapter)
  {
    super.setAdapter(paramListAdapter);
    setSelection(1);
  }

  public void setLastUpdated(CharSequence paramCharSequence)
  {
    if (paramCharSequence != null)
    {
      this.mRefreshViewLastUpdated.setVisibility(0);
      this.mRefreshViewLastUpdated.setText(paramCharSequence);
    }
    while (true)
    {
      return;
      this.mRefreshViewLastUpdated.setVisibility(8);
    }
  }

  public void setOnRefreshListener(OnRefreshListener paramOnRefreshListener)
  {
    this.mOnRefreshListener = paramOnRefreshListener;
  }

  public void setOnScrollListener(AbsListView.OnScrollListener paramOnScrollListener)
  {
    this.mOnScrollListener = paramOnScrollListener;
  }

  private class OnClickRefreshListener
  implements View.OnClickListener
  {
    public void onClick()
    {
      if (PullToRefreshListView.access$0(this.this$0) != 4)
      {
        this.this$0.prepareForRefresh();
        this.this$0.onRefresh();
      }
    }
  }

  public static abstract interface OnRefreshListener
  {
    public abstract void onRefresh();
  }
}